
remove(list=ls())

#install.packages("pacman")
library(pacman)

p_load(here, tidyverse, ggplot2, broom, haven, modelsummary, cregg, distributional,
       sjPlot, jtools, sdamr, ggpubr, survey, xtable, interactions, viridisLite, stargazer,
       ggdist, patchwork, estimatr, margins, doBy, modelr, plyr, dplyr, tidyr, ggcharts)


###FIGURE B2###
my_pal <- rcartocolor::carto_pal(n = 8, name = "Bold")[c(1, 3, 7, 2)]
yougov <- read_csv("data/yougov_tracker.csv")
pop<- dumbbell_chart(
  data = yougov,
  x = series,
  y1 = popC,
  y2 = popL,
  point_colors = c("blue2", "red2"),
  legend_labels = c("Conservative", "Labour"),
  sort=TRUE) +
  labs(
    x = NULL,
    y = NULL,
    caption = "",
    title = "Franchise popularity among UK partisans")+
  ylim(30,100)+
  theme_minimal()+
  theme(
    title = element_text(hjust = 0, face="bold"),
    legend.text = element_text(hjust = 1, size = 10),
    axis.text.y = element_text(face="bold"),
    axis.title = element_text(size=10))+
  scale_y_continuous(
    labels = scales::percent_format(scale = 1),
    limits=c(30,100))


fam<- dumbbell_chart(
  data = yougov,
  x = series,
  y1 = famC,
  y2 = famL,
  point_colors = c("blue2", "red2"),
  legend_labels = c("Conservative", "Labour"),
  sort=TRUE) +
  labs(
    x = NULL,
    y = NULL,
    caption = "Source: YouGov tracker (January 2023)",
    title = "Franchise familiarity among UK partisans")+
  theme_minimal()+
  theme(
    title = element_text(hjust = 0, face="bold"),
    legend.text = element_text(hjust = 1, size = 10),
    axis.text.y = element_text(face="bold"),
    axis.title = element_text(size=10))+
  scale_y_continuous(
    labels = scales::percent_format(scale = 1),
    limits=c(30,100))

pop/fam+
  plot_layout(guides = 'collect')

ggsave("figures_appendix/FigureB2.png", dpi=800)



###TABLE F1###
BES <- read_dta("data/BES_W19.dta")

BES <-BES%>% 
  mutate(res_party2= as.factor(res_party2))

BESmod<- lm(reject_outgroup ~ res_party2, weights=wt, data=BES)
F1<-msummary(BESmod, star=TRUE, output='latex', vcov="robust")
F1